package com.gimi.cloud.bbp.manage.service.prod.impl;

import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.exception.MsgException;
import com.gimi.cloud.bbp.common.utils.ReflectUtils;
import com.gimi.cloud.bbp.common.utils.StringUtil;
import com.gimi.cloud.bbp.commonService.common.JmOperationLogService;
import com.gimi.cloud.bbp.dao.dao.prod.EcProdCatModelDao;
import com.gimi.cloud.bbp.dao.dao.prod.EcProdCatModelValueDao;
import com.gimi.cloud.bbp.dao.dao.product.ProductSkuDao;
import com.gimi.cloud.bbp.dao.myMapper.core.query.QueryParam;
import com.gimi.cloud.bbp.dto.base.BaseDTO;
import com.gimi.cloud.bbp.dto.prod.EcProdCatModelValueDTO;
import com.gimi.cloud.bbp.entity.prod.EcProdCatModelEntity;
import com.gimi.cloud.bbp.entity.prod.EcProdCatModelValueEntity;
import com.gimi.cloud.bbp.manage.service.prod.EcProdCatModelValueService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商品模版属性值相关接口实现类
 * @author loongs
 * @Description
 * @create 2017-12-18
 * @version 1.0
 **/
@Service
public class EcProdCatModelValueServiceImpl implements EcProdCatModelValueService {
    private EcProdCatModelValueDao prodCatModelValueDao;
    private EcProdCatModelDao prodCatModelDao;
    private ProductSkuDao productSkuDao;
    private JmOperationLogService jmOperationLogService;

    @Autowired
    public EcProdCatModelValueServiceImpl(EcProdCatModelValueDao prodCatModelValueDao, EcProdCatModelDao prodCatModelDao,
                                          ProductSkuDao productSkuDao, JmOperationLogService jmOperationLogService) {
        this.prodCatModelValueDao = prodCatModelValueDao;
        this.prodCatModelDao=prodCatModelDao;
        this.jmOperationLogService=jmOperationLogService;
        this.productSkuDao=productSkuDao;
    }

    @Override
    public List<EcProdCatModelValueDTO> queryList(EcProdCatModelValueDTO dto, boolean isPaging) throws Exception {
        if(isPaging){
            PageHelper.startPage(dto.getPageNums(),dto.getPageSizes());
        }
        String sql ="SELECT val.* FROM ec_prod_cat_model_value val WHERE val.is_delete = 0 " +
                "   AND val.model_id = :modelId AND val.cm_id = :cmId AND model_value LIKE '%'||:modelValue||'%'";
        QueryParam queryParam = new QueryParam();
        queryParam.setSql(sql);
        queryParam.setParamObject(dto);
        return prodCatModelValueDao.selectListDTOBySql(queryParam);
    }

    @Override
    public EcProdCatModelValueDTO load(EcProdCatModelValueDTO dto, Object id) throws Exception {
        EcProdCatModelValueEntity prodCatModelValueEntity = prodCatModelValueDao.queryBy("cm_id",id);
        if(null == prodCatModelValueEntity){
            return null;
        }
        EcProdCatModelValueDTO prodCatModelValue = new EcProdCatModelValueDTO();
        ReflectUtils.copyPropertiesIgnoreNullAndEmptyStr(prodCatModelValueEntity,prodCatModelValue);
        return prodCatModelValue;
    }

    @Override
    public EcProdCatModelValueDTO saveOrUpdate(EcProdCatModelValueDTO dto) throws Exception {
        EcProdCatModelValueEntity prodCatModelValueEntity = null;
        if(null != dto.getCmId()){
            prodCatModelValueEntity = prodCatModelValueDao.queryBy("cm_id",dto.getCmId());
        }else{
            prodCatModelValueEntity = new EcProdCatModelValueEntity();
        }
        ReflectUtils.copyPropertiesIgnoreNullAndEmptyStr(dto,prodCatModelValueEntity);
        //判断是否能删除、禁用或者启用、编辑
        EcProdCatModelEntity isPublishedEntity = prodCatModelDao.selectProductIsPublished(prodCatModelValueEntity.getModelId());
        if(isPublishedEntity != null){
            throw new MsgException(ResponseCodeEnum.FAILURE,"保存失败，该品类模板值已上架，请确认后重试！");
        }
        int count = prodCatModelValueDao.selectCountByModelIdAndModelValueExcludeCmId(prodCatModelValueEntity.getModelId(),prodCatModelValueEntity.getModelValue(),prodCatModelValueEntity.getCmId());
        if(count > 0){
            throw new MsgException(ResponseCodeEnum.FAILURE,"保存失败，该品类模板值已存在，请确认后重试！");
        }
        if(null != prodCatModelValueEntity.getCmId()){
            prodCatModelValueDao.baseUpdateByPrimaryKey(prodCatModelValueEntity);
        }else{
            prodCatModelValueDao.baseInsert(prodCatModelValueEntity);
        }
        ReflectUtils.copyPropertiesIgnoreNullAndEmptyStr(prodCatModelValueEntity,dto);
        return dto;
    }

    @Override
    public int delete(EcProdCatModelValueDTO dto, Object id, boolean realDel) throws Exception {
        EcProdCatModelValueEntity prodCatModelValueEntity = prodCatModelValueDao.queryBy("cm_id",dto.getCmId());
        if(prodCatModelValueEntity == null){
            throw new MsgException(ResponseCodeEnum.NO_DATA,"未获取到id为"+dto.getCmId()+"的品类模版值，删除失败");
        }
        //判断是否能删除、禁用或者启用、编辑
        EcProdCatModelEntity isPublishedEntity = prodCatModelDao.selectProductIsPublished(prodCatModelValueEntity.getModelId());
        if(isPublishedEntity != null){
            throw new MsgException(ResponseCodeEnum.FAILURE,"删除失败，该品类模板值已上架，请确认后重试！");
        }
        if(!realDel){
            ReflectUtils.copyPropertiesIgnoreNullAndEmptyStr(dto,prodCatModelValueEntity);
            prodCatModelValueEntity.setIsDelete(1);
            return prodCatModelValueDao.baseUpdateByPrimaryKey(prodCatModelValueEntity);
        }
        return prodCatModelValueDao.deleteByPrimaryKey(id);
    }

    @Override
    public Integer deleteCatModelValueByCatIdExcludeCmIds(BaseDTO dto, String catId, List<Integer> cmIds) throws Exception {
        if(StringUtil.isBlank(catId)){
            throw new MsgException(ResponseCodeEnum.PARAMETER_EXCEPTION,"删除模版值失败，传入品类id为空");
        }
        String sql = "UPDATE ec_prod_cat_model_value SET is_delete = 1,update_uid = '"+dto.getUid()+ "',update_uname ='"+dto.getUname()+"' ,update_time = NOW() " +
                "  WHERE is_delete = 0 AND model_id in (SELECT model_id FROM ec_prod_cat_model WHERE is_delete = 0 AND prod_cat_id ='"+catId+"')";
        if(null != cmIds && cmIds.size() > 0){
            String cmIdStr = "";
            for (Integer cmId:cmIds){
                cmIdStr += cmId+",";
            }
            if(cmIdStr.length() > 1){
                cmIdStr= cmIdStr.substring(0,cmIdStr.length() -1);
            }
            sql+=" AND cm_id NOT in("+cmIdStr+")";
        }
        QueryParam queryParam = new QueryParam();
        queryParam.setSql(sql);
        return prodCatModelValueDao.updateSql(queryParam);
    }
}
